using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Utilities;

namespace GPX
{
    public partial class EditViewDialog : FormEx
    {
        private System.Resources.ResourceManager ResourceManager
        {
            get { return GPXAppResourceManager.ResourceManager; }
        }

        public EditViewDialog()
        {
            InitializeComponent();
        }

        public event EventHandler RedrawRequested;

        #region Properties
        private bool _DrawWaypoints;
        internal bool DrawWaypoints
        {
            get { return _DrawWaypoints; }
            set { _DrawWaypoints = value; }
        }

        private bool _DrawTrackLine;
        internal bool DrawTrackLine
        {
            get { return _DrawTrackLine; }
            set { _DrawTrackLine = value; }
        }

        private bool _DrawPhotos;
        internal bool DrawPhotos
        {
            get { return _DrawPhotos; }
            set { _DrawPhotos = value; }
        }

        private bool _DrawPointsOfInterest;
        internal bool DrawPointsOfInterest
        {
            get { return _DrawPointsOfInterest; }
            set { _DrawPointsOfInterest = value; }
        }

        private WaypointsStyle _StyleForWaypoints;
        internal WaypointsStyle StyleForWaypoints
        {
            get { return _StyleForWaypoints; }
            set { _StyleForWaypoints = value; }
        }

        private WaypointsStyle _StyleForPoI;
        internal WaypointsStyle StyleForPoI
        {
            get { return _StyleForPoI; }
            set { _StyleForPoI = value; }
        }

        private float _TrackThickness;
        internal float TrackThickness
        {
            get { return _TrackThickness; }
            set { _TrackThickness = value; }
        }

        private PhotoSize _PhotoSize;
        [System.ComponentModel.DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Hidden)]
        internal PhotoSize PhotoSize
        {
            get { return _PhotoSize; }
            set { _PhotoSize = value; }
        }

        private Color _WayPointsColor = Color.BlanchedAlmond;
        internal Color WayPointsColor
        {
            get { return _WayPointsColor; }
            set { _WayPointsColor = value; }
        }

        private Color _WayPointsHighlight = Color.Gold;
        internal Color WayPointsHighlight
        {
            get { return _WayPointsHighlight; }
            set { _WayPointsHighlight = value; }
        }

        private Color _PoIColor = Color.DarkGreen;
        internal Color PoIColor
        {
            get { return _PoIColor; }
            set { _PoIColor = value; }
        }

        private Color _TrackColor = Color.Red;
        internal Color TrackColor
        {
            get { return _TrackColor; }
            set { _TrackColor = value; }
        }

        private Color _TrackColorHighlight = Color.Goldenrod;
        internal Color TrackColorHighlight
        {
            get { return _TrackColorHighlight; }
            set { _TrackColorHighlight = value; }
        }
        #endregion Properties

        private void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                this.DialogResult = DialogResult.Cancel;
                this.Close();
            }
            catch (Exception ex)
            {
                Utils.HandleException(ex);
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                _DrawWaypoints = chkWaypoints.Checked;
                _DrawTrackLine = chkTrackline.Checked;
                _DrawPhotos = chkPhotos.Checked;
                _DrawPointsOfInterest = chkPOI.Checked;

                EnumDescConverter<WaypointsStyle> waypointStyleConverter = new EnumDescConverter<WaypointsStyle>(ResourceManager);
                string descWS = cmbWaypointStyle.Text;
                _StyleForWaypoints = waypointStyleConverter.GetEnumValueT(descWS);

                string descPS = cmbPOIStyle.Text;
                _StyleForPoI = waypointStyleConverter.GetEnumValueT(descPS);

                _TrackThickness = Convert.ToSingle(nudTrackThickness.Value);
                EnumDescConverter<PhotoSize> photosizeConverter = new EnumDescConverter<PhotoSize>(ResourceManager);
                _PhotoSize = photosizeConverter.GetEnumValueT(cmbPhotoStyle.Text);

                if (this.RedrawRequested != null)
                    this.RedrawRequested(this, null);

                if (!chkKeepOnTop.Checked)
                {
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                Utils.HandleException(ex);
            }
        }

        private void EditViewDialog_Load(object sender, EventArgs e)
        {
            Localizer.LocalizeControl(this, ResourceManager);

            EnumDescConverter<WaypointsStyle> waypointstyleConverter = new EnumDescConverter<WaypointsStyle>(ResourceManager);
            string[] waypointStyles = waypointstyleConverter.GetDescriptions().ToArray();
            cmbWaypointStyle.Items.Clear();
            cmbPOIStyle.Items.Clear();
            cmbWaypointStyle.Items.AddRange(waypointStyles);
            cmbPOIStyle.Items.AddRange(waypointStyles);

            EnumDescConverter<PhotoSize> photosizeConveter = new EnumDescConverter<PhotoSize>(ResourceManager);
            string[] photoStyles = photosizeConveter.GetDescriptions().ToArray();
            cmbPhotoStyle.Items.Clear();
            cmbPhotoStyle.Items.AddRange(photoStyles);

            chkWaypoints.Checked = _DrawWaypoints;
            chkTrackline.Checked = _DrawTrackLine;
            chkPhotos.Checked = _DrawPhotos;
            chkPOI.Checked = _DrawPointsOfInterest;

            cmbWaypointStyle.SelectedItem = waypointstyleConverter.GetEnumDescription(_StyleForWaypoints);
            cmbPOIStyle.SelectedItem = waypointstyleConverter.GetEnumDescription(_StyleForPoI);

            nudTrackThickness.Value = Convert.ToDecimal(_TrackThickness);
            cmbPhotoStyle.SelectedItem = photosizeConveter.GetEnumDescription(_PhotoSize);

            //GPX_MapForm_MapForm_Load0=Color for track line
            toolTip1.SetToolTip(btnTrackColorStandard, ResourceManager.GetString("GPX_MapForm_MapForm_Load0"));
            //GPX_MapForm_MapForm_Load1=Color for selected section of track line
            toolTip1.SetToolTip(btnTrackColorHighlight, ResourceManager.GetString("GPX_MapForm_MapForm_Load1"));
            //GPX_MapForm_MapForm_Load2=Color for Waypoints
            toolTip1.SetToolTip(btnWaypointColorStandard, ResourceManager.GetString("GPX_MapForm_MapForm_Load2"));
            //GPX_MapForm_MapForm_Load3=Color for selected Waypoints
            toolTip1.SetToolTip(btnWaypointColorHighlight, ResourceManager.GetString("GPX_MapForm_MapForm_Load3"));

            this.TopMost = true;
        }

        private void chkWaypoints_CheckedChanged(object sender, EventArgs e)
        {
            cmbWaypointStyle.Enabled = btnWaypointColorHighlight.Enabled = btnWaypointColorStandard.Enabled = chkWaypoints.Checked;
        }

        private void chkTrackline_CheckedChanged(object sender, EventArgs e)
        {
            nudTrackThickness.Enabled = btnTrackColorStandard.Enabled = btnTrackColorHighlight.Enabled = chkTrackline.Checked;
        }

        private void chkPOI_CheckedChanged(object sender, EventArgs e)
        {
            cmbPOIStyle.Enabled = btnPoIColor.Enabled = chkPOI.Checked;
        }

        private void chkPhotos_CheckedChanged(object sender, EventArgs e)
        {
            cmbPhotoStyle.Enabled = chkPhotos.Checked;
        }

        private void btnWaypointColorStandard_Click(object sender, EventArgs e)
        {
            try
            {
                System.Drawing.Color color = _WayPointsColor;
                ColorDialog colorDlg = new ColorDialog();
                colorDlg.Color = color;
                colorDlg.FullOpen = true;
                if (colorDlg.ShowDialog() == DialogResult.OK)
                {
                    _WayPointsColor = colorDlg.Color;
                }
            }
            catch (Exception ex)
            {
                Utils.HandleException(ex);
            }
        }

        private void btnWaypointColorHighlight_Click(object sender, EventArgs e)
        {
            try
            {
                System.Drawing.Color color = _WayPointsHighlight;
                ColorDialog colorDlg = new ColorDialog();
                colorDlg.Color = color;
                colorDlg.FullOpen = true;
                if (colorDlg.ShowDialog() == DialogResult.OK)
                {
                    _WayPointsHighlight = colorDlg.Color;
                }
            }
            catch (Exception ex)
            {
                Utils.HandleException(ex);
            }
        }

        private void btnTrackColorStandard_Click(object sender, EventArgs e)
        {
            try
            {
                System.Drawing.Color color = _TrackColor;
                ColorDialog colorDlg = new ColorDialog();
                colorDlg.Color = color;
                colorDlg.FullOpen = true;
                if (colorDlg.ShowDialog() == DialogResult.OK)
                {
                    _TrackColor = colorDlg.Color;
                }
            }
            catch (Exception ex)
            {
                Utils.HandleException(ex);
            }
        }

        private void btnTrackColorHighlight_Click(object sender, EventArgs e)
        {
            try
            {
                System.Drawing.Color color = _TrackColorHighlight;
                ColorDialog colorDlg = new ColorDialog();
                colorDlg.Color = color;
                colorDlg.FullOpen = true;
                if (colorDlg.ShowDialog() == DialogResult.OK)
                {
                    _TrackColorHighlight = colorDlg.Color;
                }
            }
            catch (Exception ex)
            {
                Utils.HandleException(ex);
            }
        }

        private void btnPoIColor_Click(object sender, EventArgs e)
        {
            try
            {
                System.Drawing.Color color = _PoIColor;
                ColorDialog colorDlg = new ColorDialog();
                colorDlg.Color = color;
                colorDlg.FullOpen = true;
                if (colorDlg.ShowDialog() == DialogResult.OK)
                {
                    _PoIColor = colorDlg.Color;
                }
            }
            catch (Exception ex)
            {
                Utils.HandleException(ex);
            }
        }
    }
}